package com.contest.service.impl;

import com.contest.dao.GamingQuizDao;
import com.contest.dao.UserInfoDao;
import com.contest.entity.GamingQuiz;
import com.contest.entity.UserInfo;
import com.contest.service.GamingQuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class GamingQuizServiceImpl extends  ServiceImpl<GamingQuizDao, GamingQuiz> implements GamingQuizService {

    @Autowired
    private GamingQuizDao gamingQuizDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    @Transactional
    public boolean saveGamingQuiz(GamingQuiz gamingQuiz) {
        double mon = gamingQuiz.getFdAmount().doubleValue();
        String userId = gamingQuiz.getFdUserId();
        UserInfo u = userInfoDao.selectById(userId);
        double over = u.getFdOver().doubleValue();
        if(over < mon){
            return false;
        }
        double bettingMon = u.getFdBettingTotal().doubleValue();
        u.setFdOver(new BigDecimal(over - mon));
        u.setFdBettingTotal(new BigDecimal(bettingMon + mon));
        int num = userInfoDao.updateById(u);
        if(num > 0){
            int n = gamingQuizDao.insert(gamingQuiz);
            return true;
        }
        return false;
    }
}
